package com.icss.ah.designatedSales.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.icss.ah.base.bean.EasyuiDataGridJson;

@Service
public class DesignatedSalesService {
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	private EasyuiDataGridJson dataGridJson = new EasyuiDataGridJson();
	
	/**
	 * 选点商品投放
	 */
	public EasyuiDataGridJson queryDesignatedSalesList(Map<String, String> map){
		try {
			List<Map<String, Object>> rows = sqlSessionTemplate.selectList("DesignatedSalesBean.queryDesignatedSalesList",map);
			dataGridJson.setRows(rows);
			Long total = sqlSessionTemplate.selectOne("DesignatedSalesBean.queryDesignatedSalesListCount",map);
			dataGridJson.setTotal(total);
			infolog.info("DesignatedSalesService|queryDesignatedSalesList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DesignatedSalesService|queryDesignatedSalesList",e);
		}
		return dataGridJson;
	}
	
	/**
	 * 客户查询
	 */
	public EasyuiDataGridJson queryClientInfoList(Map<String, String> map){
		try {
			String productId = "'" + map.get("productId").replace(",", "','") + "'";
			map.put("productId", productId);
			List<Map<String, Object>> rows = sqlSessionTemplate.selectList("DesignatedSalesBean.queryClientInfoList",map);
			dataGridJson.setRows(rows);
			Long total = sqlSessionTemplate.selectOne("DesignatedSalesBean.queryClientInfoListCount",map);
			dataGridJson.setTotal(total);
			infolog.info("DesignatedSalesService|queryClientInfoList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DesignatedSalesService|queryClientInfoList",e);
		}
		return dataGridJson;
	}
	
	public EasyuiDataGridJson queryClientInfoList1(Map<String, String> map){
		try {
			String productId = "'" + map.get("productId").replace(",", "','") + "'";
			map.put("productId", productId);
			List<Map<String, Object>> rows = new ArrayList<Map<String,Object>>();
			Long total = sqlSessionTemplate.selectOne("DesignatedSalesBean.queryClientInfoListCount1",map);
			Long total2 = sqlSessionTemplate.selectOne("DesignatedSalesBean.queryClientInfoListCount2",map);
			long startIndex = Long.valueOf(map.get("startIndex"));
			long rowNums = Long.valueOf(map.get("rowNums"));
			if(startIndex<total){
				List<Map<String, Object>> rows1 = sqlSessionTemplate.selectList("DesignatedSalesBean.queryClientInfoList1",map);
				rows.addAll(rows1);
			}
			long size = rows.size();
			if(size<rowNums){
				long index = startIndex-total<0 ? 0L : startIndex-total;
				map.put("startIndex", Long.toString(index));
				map.put("rowNums", Long.toString(rowNums-size));
				List<Map<String, Object>> rows2 = sqlSessionTemplate.selectList("DesignatedSalesBean.queryClientInfoList2",map);
				rows.addAll(rows2);
			}
			for(int i=0;i<rows.size();i++){
				rows.get(i).put("PID", startIndex+i+1);
			}
			dataGridJson.setRows(rows);
			dataGridJson.setTotal(total+total2);
			infolog.info("DesignatedSalesService|queryClientInfoList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DesignatedSalesService|queryClientInfoList",e);
		}
		return dataGridJson;
	}
	
	/**
	 * 保存选点投放客户
	 */
	public String saveClientInfo(String corpId,String shopId,String productId,String licenceNoStr){
		String result = "添加失败";
		try {
			String [] proArr = productId.split(",");
			String [] lnArr = licenceNoStr.split(",");
			List<Map<String, String>> list = new ArrayList<Map<String,String>>();
			for(String pro : proArr){
				for(String ln : lnArr){
					String type = ln.substring(0, 1);
					String licenceNo = ln.substring(1);
					Map<String, String> map = new HashMap<String, String>();
					map.put("uuid", UUID.randomUUID().toString());
					map.put("corpId", corpId);
					map.put("shopId", shopId);
					map.put("productId", pro);
					map.put("licenceNo", licenceNo);
					map.put("buyerType", type);
					list.add(map);
				}
			}
			sqlSessionTemplate.insert("DesignatedSalesBean.saveClientInfo", list);
			result = "添加成功";
			infolog.info("DesignatedSalesService|saveClientInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DesignatedSalesService|saveClientInfo",e);
		}
		return result;
	}
	
	/**
	 * 删除指定选点投放客户
	 */
	public String delDesignatedSalesInfo(String uuid){
		String result = "删除失败";
		try {
			String[] arr = uuid.split(",");
			for (String u : arr) {
				sqlSessionTemplate.delete("DesignatedSalesBean.delDesignatedSalesInfo",u);
			}
			result = "删除成功";
			infolog.info("DesignatedSalesService|delDesignatedSalesInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DesignatedSalesService|delDesignatedSalesInfo",e);
		}
		return result;
	}
	
	/**
	 * 选点客户查询
	 */
	public EasyuiDataGridJson queryBuyerInfoList(Map<String, String> map){
		try {
			List<Map<String, Object>> rows = sqlSessionTemplate.selectList("DesignatedSalesBean.queryBuyerInfoList",map);
			dataGridJson.setRows(rows);
			Long total = sqlSessionTemplate.selectOne("DesignatedSalesBean.queryBuyerInfoListCount",map);
			dataGridJson.setTotal(total);
			infolog.info("DesignatedSalesService|queryBuyerInfoList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DesignatedSalesService|queryBuyerInfoList",e);
		}
		return dataGridJson;
	}

}
